import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'

// 加载 element 组件库
import ElementUI from 'element-ui'

// 加载 element 组件库的样式
import 'element-ui/lib/theme-chalk/index.css'

// 加载全局样式文件
import './styles/index.less'
import 'element-ui/lib/theme-chalk/display.css'

// tiptap
import { ElementTiptapPlugin } from 'element-tiptap'
// import this package's styles
import 'element-tiptap/lib/index.css'

Vue.config.productionTip = false

// 全局注册 element 组件库
Vue.use(ElementUI)

// use this package's plugin
Vue.use(ElementTiptapPlugin, { lang: 'zh' })
// Now you register `'el-tiptap'` component globally.

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

// 刷新保存状态
if (sessionStorage.getItem('store')) {
  store.replaceState(
    Object.assign(
      {},
      store.state,
      JSON.parse(sessionStorage.getItem('store')),
      sessionStorage.removeItem('store')
    )
  )
  sessionStorage.removeItem('store')
}

// 监听，在页面刷新时将vuex里的信息保存到sessionStorage里
window.addEventListener('beforeunload', () => {
  sessionStorage.setItem('store', JSON.stringify(store.state))
})
